my $fname = 'forum.mysql';
my $prefix = 'FM_';
my $host = '__hidden__';
my $user = '__hidden__';
my $pass = '__hidden__';
my $db = '__hidden__';
my $sock = '__hidden__';

print <<PRINT;
Are you sure you want to COMPLETELY reset the forum database?
This will wipe out 100% of everything with no backups or anything
You better be ABOSULTELY SURE about what you're doing
If you are, then type in the password of the administrator
PRINT


(my $line = <STDIN>) =~ s/\r|\n//g;

use DBI;
my $dbh = DBI->connect("DBI:mysql:$db:$host:mysql_socket=$sock", $user, $pass,
						{AutoCommit=>1});
my $sth=$dbh->prepare("SELECT*FROM FM_USERS WHERE PRIV='A'AND PASSWD=HEX(?)");

my $truth;
if ($sth && $sth->execute($line))
{
	$truth = ($sth->fetchrow_arrayref() ? 1 : 0); # DBI is stupid
}
else
{
	print <<PRINT;
It seems that the forum doesn't even exist. Would you like to clear
the database anyway? (Type exactly this: yes)
PRINT
	($line = <STDIN>) =~ s/[\r\n]//g;
	$truth = $line eq 'yes';
}
#$dbh->{RaiseError} = $dbh->{PrintError} = 1;

if (!$truth)
{
	print "Database was not cleared\n";
	exit;
}

$sth = $dbh->prepare('SHOW TABLES');
$sth->execute();
while (my $table = $sth->fetchrow())
{
	my $query = "DROP TABLE $table";
	$dbh->do($query), print "$query\n" if ($table =~ /^$prefix/);
}
$sth = $dbh->prepare('SHOW PROCEDURE STATUS');
$sth->execute();
while (my $row = $sth->fetchrow_hashref())
{
	my $proc = $row->{Name};
	my $query = "DROP PROCEDURE $proc";
	$dbh->do($query), print "$query\n" if ($proc =~ /^$prefix/);
}
print"Done clearing forum. Would you like to initialize it? (y/n)\n";
chomp($line = <STDIN>);
if ($line !~ /^y/i)
{
	print "Fine then, initialize it yourself\n";
	exit;
}


`echo 'source $fname' | ./login.bash`;
if ($?)
{
	print "Error: $!";
}
else
{
	print 'Yay, re-initialized it!';
}
print "\n";
